package com.hzp.lolnews.activity.view;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.hzp.lolnews.R;
import com.hzp.lolnews.base.BaseActivity;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.RequestSMSCodeListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.VerifySMSCodeListener;

/**
 * Created by Administrator on 2016/10/24 0024.
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener, ILoginView {

    private EditText edReName;
    private EditText edReYzn;
    private EditText edPassword;
    private String name;

    @Override
    protected void initView() {
        edReName = (EditText) findViewById(R.id.sign_input_phone);
        edReYzn = (EditText) findViewById(R.id.sign_input_Code);
        Button btnYzn = (Button) findViewById(R.id.sign_input_getCode);
        ImageView sign_back = (ImageView) findViewById(R.id.sign_back);
        sign_back.setOnClickListener(this);
        btnYzn.setOnClickListener(this);
        edPassword = (EditText) findViewById(R.id.register_input_pwd);
        Button btnRegister = (Button) findViewById(R.id.sign);
        btnRegister.setOnClickListener(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_regist;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sign_back:
                finish();
                break;
            case R.id.sign_input_getCode:
                name = edReName.getText().toString().trim();
                BmobSMS.requestSMSCode(this, name, "模板", new RequestSMSCodeListener() {
                    @Override
                    public void done(Integer integer, BmobException e) {
                        if (e == null) {
                            Toast.makeText(RegisterActivity.this, "发送成功，请查收", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(RegisterActivity.this, "发送失败，请检查网络", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
            case R.id.sign:
                String yzm = edReYzn.getText().toString().trim();
                final String password = edPassword.getText().toString().trim();
                BmobSMS.verifySmsCode(this, name, yzm, new VerifySMSCodeListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e != null){
                            if (TextUtils.isEmpty(name) || TextUtils.isEmpty(password)) {
                                Toast.makeText(RegisterActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            // 使用BmobSDK提供的注册功能
                            BmobUser user = new BmobUser();
                            user.setUsername(name);
                            user.setPassword(password);
                            user.signUp(RegisterActivity.this, new SaveListener() {
                                @Override
                                public void onSuccess() {
                                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                                    startActivity(intent);
                                }

                                @Override
                                public void onFailure(int i, String s) {
                                    Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                                }
                            });
                        } else {
                            Toast.makeText(RegisterActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                break;
        }
    }

    @Override
    public String getUserName() {
        return null;
    }

    @Override
    public String getPassWord() {
        return null;
    }

    @Override
    public void register() {

    }

    @Override
    public void toSucceedActivity() {

    }

    @Override
    public void showLoginFailedView() {

    }
}
